<?php

namespace app\models;

use app\behaviors\ApplyNotifyBehavior;
use app\components\BaseActiveRecord;
use app\events\ApplyNotifyEvent;
use yii\base\Event;

/**
 * This is the model class for table "{{%activity_apply}}".
 *
 * @property integer $id
 * @property integer $type
 * @property integer $aid
 * @property integer $uid
 * @property integer $pub_uid
 * @property integer $status
 * @property string $uname
 * @property string $phone
 * @property string $school
 * @property string $remark
 * @property integer $ctime
 *
 * @property User $user
 * @property Activity $activity
 */
class ActivityApply extends BaseActiveRecord {

    const STATUS_PENDING = 1;
    const STATUS_DENY = 2;
    const STATUS_ALLOW = 3;
    const EVENT_APPLY_NOTIFY = 'applyNotify';

    public static $statusLabels = [
        self::STATUS_PENDING => '待审核',
        self::STATUS_DENY => '审核被拒',
        self::STATUS_ALLOW => '审核通过'
    ];

    public static $statusColors = [
        self::STATUS_PENDING => 'pending',
        self::STATUS_DENY => 'deny',
        self::STATUS_ALLOW => 'allow'
    ];

    public function behaviors() {
        return [
            'class' => ApplyNotifyBehavior::className()
        ];
    }

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%activity_apply}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['school', 'uname', 'remark'], 'string', 'max' => 160],
            [['phone'], 'string', 'max' => 11],
            ['type', 'in', 'range' => [Activity::TYPE_JOB, Activity::TYPE_ACTIVITY]],
            [['type', 'aid', 'uid', 'pub_uid', 'status', 'ctime'], 'integer']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'type' => 'Type',
            'aid' => 'Own ID',
            'uid' => '用户id',
            'pub_uid' => '发布者uid',
            'uname' => '用户名',
            'phone' => '电话',
            'school' => '学校',
            'status' => '状态',
            'ctime' => '申请时间',
            'remark' => '拒绝原因'
        ];
    }

    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'uid']);
    }

    public function getActivity() {
        return $this->hasOne(Activity::className(), ['id' => 'aid']);
    }

    public static function statusAllowAndDeny($id, User $user, $status, $remark) {
        $model = static::findOne($id);
        if (!$model) {
            return static::retErr('报名信息不存在');
        }
        if ($user->id != $model->pub_uid) {
            return static::retErr('非法操作');
        }
        if (!$model->getIsPending()) {
            return static::retErr('报名已经审核过了');
        }

        $activity = Activity::findOne($model->aid);
        if (!$activity) {
            return static::retErr('活动不存在');
        }

        if ($activity->number > 0 && $activity->apply_number < $activity->number) {
            $activity->apply_number = $activity->apply_number + 1;
            $activity->save();
        }

        $model->status = $status;
        $model->remark = $remark;
        $res = $model->save();
        if ($res['code'] != 0) {
            return $res;
        }

        $event = new ApplyNotifyEvent();
        $event->activity = $activity;
        $event->activityApply = $model;
        $model->trigger(self::EVENT_APPLY_NOTIFY, $event);

        return $res;
    }

    public static function getApplyList($aid) {
        $where = ['aid' => $aid];
        $where['status'] = static::STATUS_ALLOW;
        return static::find()
            ->where($where)
            ->with(['user'])->asArray()->all();
    }

    public static function saveApply(Activity $activity, User $user, $status = self::STATUS_PENDING) {
        $model = new ActivityApply();
        $model->aid = $activity->id;
        $model->uid = $user->id;
        $model->pub_uid = $activity->uid;
        $model->type = $activity->type;
        $model->status = $status;
        $model->uname = $user->uname;
        $model->phone = $user->phone;
        $model->school = $user->profile->school;
        return $model->save();
    }

    public static function getIsApply($aid, $uid) {
        $where = ['aid' => $aid];
        $where['uid'] = $uid;
        return static::find()->where($where)->count() > 0;
    }

    public function extraFields() {
        $fields = parent::extraFields();
        $fields['avatar'] = function (ActivityApply $model) {
            return $model->user->getAvatar();
        };
        return $fields;
    }

    public function getIsPending() {
        return intval($this->status) === intval(self::STATUS_PENDING);
    }

    public function getIsDeny() {
        return intval($this->status) === intval(self::STATUS_DENY);
    }

    public function getIsAllow() {
        return intval($this->status) === intval(self::STATUS_ALLOW);
    }

    public function getIsActivity() {
        return intval($this->type) === intval(Activity::TYPE_ACTIVITY);
    }
}
